/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of GW-Android.

GW-Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GW-Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GW-Android. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.android.components.map;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Pair;
import android.view.Gravity;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("unchecked")
public class GeoObjectItemizedOverlay extends ItemizedOverlay {

	private List<Pair<GeoObject, OverlayItem>> mObjects = new ArrayList<Pair<GeoObject, OverlayItem>>();
	private Context mContext;

	public GeoObjectItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenter(defaultMarker));
		mContext = context;
	}
	
	public void addGeoObject(GeoObject object) {
		OverlayItem overlay = new OverlayItem(object.getGeoPoint(), object.getLabel(), object.getReference());
		mObjects.add(Pair.create(object, overlay));
		this.populate();
	}
	
	private GeoObject getGeoObject(int index) {
		return mObjects.get(index).first;
	}
	
	private OverlayItem getOverlayItem(int index) {
		return mObjects.get(index).second;
	}

	@Override
	protected OverlayItem createItem(int i) {
		return getOverlayItem(i);
	}
	
	@Override
	public int size() {
		return mObjects.size();
	}

	@Override
	public boolean onTap(int index) {
		new AlertDialog.Builder(mContext)
		.setIcon(getGeoObject(index).getMarker())
		.setTitle(getGeoObject(index).getLabel())
		// TODO O ideal é que esta View seja gerada de acordo com tipo do GeoObject;
		// i.e. se for uma foto, exibimos a foto de uma vez.
		.setView(buildLink(index))
		.setMessage(calcCoordinates(index))
		.setPositiveButton("Close", 
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) { }
		}).show();
		return true;
	}
	
	private String calcCoordinates(int index) {
		GeoPoint point = getGeoObject(index).getGeoPoint();
		double lat = point.getLatitudeE6() / 1E6;
		double lon = point.getLongitudeE6() / 1E6;
		return "Latitude:  " + lat + "\nLongitude:  " + lon;
	}

	private TextView buildLink(int index) {
		String snippet = getGeoObject(index).getReference();
		if (snippet == null) return null;  // Trata o caso em que o GeoObject não contém uma referência.
		SpannableString s = new SpannableString(snippet);
		Linkify.addLinks(s, Linkify.WEB_URLS);
		TextView message = new TextView(mContext);
		message.setText(s);
		message.setMovementMethod(LinkMovementMethod.getInstance());
		message.setGravity(Gravity.CENTER);
		return message;
	}
}